package demo2;

public class MyLinkedList implements IList{
    static class ListNode {
        public int val;

        public ListNode prev;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
   public ListNode head;
   public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
      if(head == null){
          head = node;
          last = node;
      }else {
          last.next = node;
          node.prev = last;
          last = node;
      }
    }

    @Override
    public void addIndex(int index, int data) {
     int ret = size();
     if(index < 1 || index > ret){
         return;
     }
     if(index == 1){
            addFirst(data);
            return;
     }
     ListNode cur = this.head;
     for(int i = 0 ; i < index - 1; i++){
         cur = cur.next;
     }
     ListNode node = new ListNode(data);
     node.next = cur;
     node.prev = cur.prev;
     cur.prev.next = node;
     cur.prev = node;
    }


    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
//需要
    @Override
    public void remove(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                //头
                if(head == cur){
                    head = head.next;
                    if(head == null){
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    if(cur.next == null){
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                //中

                //尾
            }
            cur = cur.next;
        }
    }
    public void remove1(int key) {
        if(head == null){
            return;
        }
      ListNode head1 = this.head;
          //头
          if(head1.val == key){
              head = head.next;
              if(head == null){
                  last = null;
              }else {
                  head1.prev = null;
              }
              head1 = head1.next;

              return;
          }
          //中间
          while(head1.next != null){
              if(head1.val == key){
                  head1.prev.next = head1.next;
                  head1.next.prev = head1.prev;
                  return;
              }
              head1 = head1.next;
          }
          //尾巴
          if(head1.val == key){
              last = last.prev;
              head1.prev.next = null;
          }
    }
// 需要
    @Override
    public void removeAllKey(int key) {
        ListNode cur = this.head;
        if (head == null) {
            return;
        }
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
                } else if (cur.next == null) {
                    last = last.prev;
                    cur.prev.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display1() {
        ListNode cur = this.last;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.prev;
        }
        System.out.println();
    }

    //需要
    @Override
    public void clear() {
      while(head.next != null){
          ListNode cur = head.next;
          head.next = null;
          head.prev = null;
          head = cur;
      }
      head = null;
      last = null;
    }
}
